// ==UserScript==
// @name FreeTurbo
// @description Extend Turbik.TV with free content 
// @author Pavel Pirogov
// @license MIT
// @version 1.1
// @include https://turbik.tv/Watch/*
// ==/UserScript==	
(function (window, undefined) {
    var w;
    if (typeof unsafeWindow != undefined) {
        w = unsafeWindow
    } else {
        w = window;
    }
    if (w.self != w.top) {
        return;
    }
	
    if (/https:\/\/turbik.tv\/Watch/.test(w.location.href)) {
		var urls = {
			seasonvarSerialSearch: "http://go.mail.ru/search_site?num=10&p=1&rch=l&aux=Tj7dGd&sf=0&x=0&y=0&q=",
			torrentLink: "http://torrentz.com/search?q={name}+s{season}e{episode}"
		};
		var data = {};
		var main = document.getElementsByClassName('main')[0];
		var names = main.getElementsByClassName('mains')[0].getElementsByTagName('a');
		data.nameEN = names[0].innerText.trim();
		data.nameRU = names[1].innerText.trim();
		var rawTitle = main.getElementsByClassName('maine')[0].innerText;
		var tmp = rawTitle.split("Эпизод: ");
		var titles = tmp[0].split("/");
		data.titleEN = titles[0].trim();
		data.titleRU = titles[1].trim();
		var numbers = tmp[1].split(", Сезон: ");
		data.episode = numbers[0].trim();
		data.season = numbers[1].trim();
		var container = document.getElementsByClassName('vdfixbox')[0];
		var panel = document.createElement('div');
		panel.className = "freeturbo";
		panel.style.paddingLeft = '22px';
		container.insertBefore(panel, container.firstChild);
		var titlePanel = document.createElement('span');
		titlePanel.innerHTML = "&nbsp;FreeTurbo v1.1 предлагает: ";
		panel.appendChild(titlePanel);
		
		var seasonvarLink = document.createElement('a');
		seasonvarLink.innerHTML = " смотреть " + data.nameEN + " на seasonvar.ru ";
		seasonvarLink.href = urls.seasonvarSerialSearch + data.nameEN;
		seasonvarLink.target = "_BLANK";
		panel.appendChild(seasonvarLink);
		
		var orSpan = document.createElement('span');
		orSpan.innerHTML = " или ";
		panel.appendChild(orSpan);
		
		var torrentLink = document.createElement('a');
		torrentLink.innerHTML = " качать torrent этой серии ";
		torrentLink.href = urls.torrentLink.replace("{name}", data.nameEN).replace("{season}", data.season).replace("{episode}", data.episode);
		torrentLink.target = "_BLANK";
		panel.appendChild(torrentLink);
		
		var endSpan = document.createElement('span');
		endSpan.innerHTML = " если тут не грузится. ";
		panel.appendChild(endSpan);
		
    }
})(window);